package com.neusoft.hifly.mybatis.handler;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.utils.GlobalConstants;
import com.neusoft.hifly.core.utils.OrganCodeContextHolder;

/**
 * Title: 核心类库
 * <p>
 * Description: 动态表名 SQL 解析器
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service
public class MyTableNameHandler implements ITableNameHandler {

	@Override
	public String dynamicTableName(final MetaObject metaObject, final String sql, String tableName) {
		final String organCode = OrganCodeContextHolder.get();
		if (StringUtils.isNotEmpty(organCode)) {
			// TODO 【技巧】MyBatisPlus替换数据表名
			Log.debug("MyBatisPlus替换数据表名 : " + organCode);
			tableName = tableName.replaceAll(GlobalConstants.DYNAMIC_TABLE_NAME_PREFIX, organCode);

		}
		return tableName;
	}

}
